package com.ev.scm.service.impl;

import com.ev.apis.model.DsResultResponse;
import com.ev.common.enums.AuditStatusDict;
import com.ev.framework.config.Constant;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.framework.utils.R;
import com.ev.framework.utils.StringUtils;
import com.ev.scm.dao.ProjectDao;
import com.ev.scm.domain.ProjectDO;
import com.ev.scm.service.ProjectService;
import com.ev.scm.vo.ProjectPageParam;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private MessageSourceHandler messageSourceHandler;


    @Override
    public ProjectDO get(Long id) {
        return projectDao.get(id);
    }

    @Override
    public List<ProjectDO> list(Map<String, Object> map) {
        return projectDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return projectDao.count(map);
    }

    @Override
    public int save(ProjectDO project) {
        return projectDao.save(project);
    }

    @Override
    public int update(ProjectDO project) {
        return projectDao.update(project);
    }

    @Override
    public int updateAll(ProjectDO project) {
        return projectDao.updateAll(project);
    }

    @Override
    public int remove(Long id) {
        return projectDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return projectDao.batchRemove(ids);
    }

    @Override
    public R saveAndVerify(ProjectDO saveParam) {
        List<ProjectDO> projectDOList = this.checkSave(saveParam);
        if (projectDOList.size() > 0) {
            if (projectDOList.get(0).getDelFlag()==1){
                Map<String, Object> result = Maps.newHashMapWithExpectedSize(1);
                result.put("id", projectDOList.get(0).getId());
                return R.error(R.DEL_ERROR, messageSourceHandler.getMessage("common.duplicate.delete.existed", null), result);
            }

            return R.error(messageSourceHandler.getMessage("common.duplicate.serialNoOrName",null));
        }

        if (saveParam.getId() != null) {
            if (Objects.equals(projectDao.get(saveParam.getId()).getAuditSign(),AuditStatusDict.OK_AUDIT.getId())){
                return R.error(messageSourceHandler.getMessage("common.approved.update.disabled",null));
            }
            saveParam.setDelFlag(0);
            return projectDao.update(saveParam) > 0 ? R.ok() : R.error();
        }
        saveParam.setAuditSign(AuditStatusDict.WAIT_AUDIT.getId());
        return projectDao.save(saveParam) > 0 ? R.ok() : R.error();
    }

    private List<ProjectDO> checkSave(ProjectDO saveParam) {
        return projectDao.checkSave(saveParam);
    }

    @Override
    public Map<String, Object> getById(Long id) {
        ProjectDO project = projectDao.get(id);
        Map<String, Object> result = Maps.newHashMap();
        if (project !=null){
            result.put("data",project);
            return result;
        }
        return result;
    }

    @Override
    public Map<String, Object> pageList(ProjectPageParam pageParam) {
        Map<String, Object> result = Maps.newHashMap();
        pageParam.setName(StringUtils.sqlLike(pageParam.getName()));
        List<Map<String, Object>> data = projectDao.pageList(pageParam);
        if (data.size() > 0) {
            int total = projectDao.pageCount(pageParam);
            result.put("data", new DsResultResponse(pageParam.getPageno(), pageParam.getPagesize(), total, data));
        }
        return result;
    }

    @Override
    public int batchUpdate(List<ProjectDO> param) {
        return projectDao.batchUpdate(param);
    }

    @Override
    public R audit(Long id) {
        ProjectDO projectDO = this.get(id);
        if (Objects.equals(projectDO.getAuditSign(), Constant.OK_AUDITED)) {
            return R.error(messageSourceHandler.getMessage("common.duplicate.approved",null));
        }
        projectDO.setId(id);
        projectDO.setAuditSign(Constant.OK_AUDITED);
        projectDO.setAuditTime(new Date());
        this.updateAll(projectDO);

        return R.ok();
    }

    @Override
    public R reverseAudit(Long id) {
        ProjectDO projectDO = this.get(id);
        if (Objects.equals(projectDO.getAuditSign(), Constant.WAIT_AUDIT)) {
            return R.error(messageSourceHandler.getMessage("receipt.reverseAudit.nonWaitingAudit",null));
        }
        projectDO.setId(id);
        projectDO.setAuditSign(Constant.WAIT_AUDIT);
        projectDO.setAuditTime(null);
        this.updateAll(projectDO);

        return R.ok();
    }

    @Override
    public List<ProjectDO> getByIds(Long[] toArray) {
        return projectDao.getByIds(toArray);
    }

}
